<?php

class Db_Tables_MembersJoinedItems extends Zend_Db_Table
{
    protected $_name = 'members-joined-items';
	var $member_id;
	var $level3_id;
	
	function AddRecord()
	{
		$result = FALSE;
		
		if($this->member_id && $this->level3_id)
		{
			$row = $this->fetchRow("member_id=".$this->member_id." and level3_id=".$this->level3_id);
			
			if(!$row)
			{
				$mod_level3 = new Db_Tables_Level3();
				$level3_row = $mod_level3->fetchRow("id=".$this->level3_id);
				
				if($level3_row)
				{
					$new_row = $this->createRow();
					$new_row->member_id = $this->member_id;
					$new_row->level1_id = $level3_row->level1_id;
					$new_row->level2_id = $level3_row->level2_id;
					$new_row->level3_id = $level3_row->id;
					$new_row->ctime = date("Y-m-d H:i:s");
					if($new_row->save())
					{
						$result = TRUE;
					}
				}
			}
		}
		
		return $result;
	}
	
	function AddJoinedMembers()
	{
		$result = FALSE;
		
		if($this->level3_id)
		{
			$mod_level3 = new Db_Tables_Level3();
			$level3_row = $mod_level3->fetchRow("id=".$this->level3_id);
			$level3_row->joined_members = $mod_level3->joined_members + 1;
			$level3_row->save();

			$mod_level_2 = new Db_Tables_Level2();
			$level2_row = $mod_level_2->fetchRow("id=".$level3_row->level2_id);
			$level2_row->joined_members = $level2_row->joined_members + 1;
			$level2_row->save();

			$mod_level_1 = new Db_Tables_Level1();
			$level1_row = $mod_level_1->fetchRow("id=".$level3_row->level1_id);
			$level1_row->joined_members = $level1_row->joined_members + 1;
			$level1_row->save();

			$result = TRUE;
		}

		return $result;
	}
}